const queue = [];
let requesting = false;

const limitRequest = request => args =>
	new Promise((resolve, reject) => {
		const dispatch = () => {
			requesting = true;
			request(args)
				.then(resolve, reject)
				.finally(() => {
					const fn = queue.shift();
					if (queue.length === 0) {
						requesting = false;
					}
					fn && fn();
				});
		};
		if (requesting) {
			queue.push(dispatch);
		} else {
			dispatch();
		}
	});
export default limitRequest;
